#ifndef WRLINFO_H
#define WRLINFO_H

#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include <nlohmann/json.hpp>  // 引入 nlohmann json 库
#include "PartURDF.h"

/** 启动小严程序  所用的结构 **/
class WrlInfo {
public:
    // std::string urdf;
    int index;
    int workID;
    std::string name;
    std::string current_path;

    void GetDataByHubo(const WRLJson &hubo)
    {
        current_path = hubo.dir_path;
        index = hubo.wrlid;
        workID = hubo.workstation;
        name = hubo.wrl_name;
    }

    // 序列化为 JSON
    friend void to_json(nlohmann::json& j, const WrlInfo& wrl) {
        j = nlohmann::json{
            {"index", wrl.index},
            {"workID", wrl.workID},
                {"name", wrl.name},
            {"current_path", wrl.current_path}
        };
    }

    // 从 JSON 反序列化
    friend void from_json(const nlohmann::json& j, WrlInfo& wrl) {
        j.at("index").get_to(wrl.index);
        j.at("workID").get_to(wrl.workID);
        j.at("name").get_to(wrl.name);
        j.at("current_path").get_to(wrl.current_path);
    }
};

// WrlInfoMap 的类型定义
typedef std::map<int, WrlInfo> WrlInfoMap;

// 序列化 WrlInfoMap 到文件
void to_json(const std::string& filename, const WrlInfoMap& map) {
    nlohmann::json j;
    for (const auto& [key, value] : map) {
        j[std::to_string(key)] = value;  // 将 map 中的 key 转为字符串
    }

    std::ofstream file(filename);
    if (file.is_open()) {
        file << j.dump(4);  // 格式化输出 JSON
        file.close();
        std::cout << "WrlInfoMap 已序列化写入到 " << filename << std::endl;
    } else {
        std::cerr << "WrlInfoMap 无法写入Json!" << std::endl;
    }
}

// 从文件中反序列化 WrlInfoMap
void from_json(const std::string& filename, WrlInfoMap& map) {
    std::ifstream file(filename);
    if (file.is_open()) {
        nlohmann::json j;
        file >> j;
        file.close();

        for (auto& [key, value] : j.items()) {
            int id = std::stoi(key);  // 将字符串转换回 int
            map[id] = value.get<WrlInfo>();
        }

        std::cout << "WrlInfoMap 已序列化 from " << filename << std::endl;
    } else {
        std::cerr << "Unable to open file for reading!" << std::endl;
    }
}


class DBStruct {
public:
    std::string diban_json;
    std::string mhd_json;
    std::string up_json;
    std::string down_json;
    std::string name;

    std::string path;
    int index;  // 顺序?
    int workID; //工件ID
    int partALL; // 筋板数量

    // 通过 json 库提供的 from_json 函数反序列化
    friend void to_json(nlohmann::json& j, const DBStruct& db) {
        j = nlohmann::json{
            {"diban_json", db.diban_json},
            {"mhd_json", db.mhd_json},
                {"up_json", db.up_json},
                {"down_json", db.down_json},
            {"name", db.name},
                {"path", db.path},
            {"partALL", db.partALL},
            {"index", db.index},
            {"workID", db.workID}
        };
    }

    // 通过 json 库提供的 to_json 函数序列化
    friend void from_json(const nlohmann::json& j, DBStruct& db) {
        j.at("diban_json").get_to(db.diban_json);
        j.at("mhd_json").get_to(db.mhd_json);
        j.at("name").get_to(db.name);
        j.at("partALL").get_to(db.partALL);
        j.at("index").get_to(db.index);
        j.at("workID").get_to(db.workID);

        j.at("up_json").get_to(db.up_json);
        j.at("down_json").get_to(db.down_json);
        j.at("path").get_to(db.path);
    }
};

// DBStructMap 的序列化与反序列化
typedef std::map<int, DBStruct> DBStructMap;

// 序列化 DBStructMap 到文件
static void to_json(const std::string& filename, const DBStructMap& map) {
    nlohmann::json j;
    for (const auto& [key, value] : map) {
        j[std::to_string(key)] = value;  // 将 map 中的 key 转为字符串
    }

    std::ofstream file(filename);
    if (file.is_open()) {
        file << j.dump(4);  // 格式化输出 JSON
        file.close();
        std::cout << "DBStructMap 已序列化写入到 " << filename << std::endl;
    } else {
        std::cerr << "DBStructMap 无法保存到文件!" << std::endl;
    }
}

// 从文件中反序列化 DBStructMap
static void from_json(const std::string& filename, DBStructMap& map) {
    std::ifstream file(filename);
    if (file.is_open()) {
        nlohmann::json j;
        file >> j;
        file.close();

        for (auto& [key, value] : j.items()) {
            int id = std::stoi(key);  // 将字符串转换回 int
            map[id] = value.get<DBStruct>();
        }

        std::cout << "DBStructMap 已序列化 from " << filename << std::endl;
    } else {
        std::cerr << "DBStructMap 无法从文件读取:" << filename << std::endl;
    }
}

#endif
